/*
 * Copyright 2007 www.luzand.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Logger
 */
function yrprxLogger(prefix)
{
    this.prefix = prefix ? new String(prefix) : "";

    this.enabled = false; //by default
    // check preferences
    try
    {
        var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

        if (pref.prefHasUserValue("browser.dom.window.dump.enabled") && pref.prefHasUserValue("extensions.webslideshow.logging.enable"))
            this.enabled = pref.getBoolPref("browser.dom.window.dump.enabled") && pref.getBoolPref("extensions.webslideshow.logging.enable");
    }
    catch(ex)
    {
    }
}

yrprxLogger.prototype.exceptionToString = function(ex)
{
    return ex ? "Exception: " + ex : "";
}

yrprxLogger.prototype.debug = function(message, e)
{
    if (!this.enabled)
        return;

    try
    {
        dump("DEBUG " + this.prefix + " - " + message + this.exceptionToString(e) + "\n");
    }
    catch(ex)
    {
    }
}

yrprxLogger.prototype.warn = function(message, e)
{
    if (!this.enabled)
        return;

    try
    {
        dump("WARN " + this.prefix + " - " + message + this.exceptionToString(e) + "\n");
    }
    catch(ex)
    {
    }
}

yrprxLogger.prototype.error = function(message, e)
{
    if (!this.enabled)
        return;

    try
    {
        dump("ERROR " + this.prefix + " - " + message + this.exceptionToString(e) + "\n");
    }
    catch(ex)
    {
    }
}

yrprxLogger.prototype.dumpProperties = function(value)
{
    if (!this.enabled)
        return;

    for (var key in value)
    {
        this.debug(key + " : " + value[key]);
    }
}
